﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering_Rules;
using Catering.View_Operaction;
using System.Data;
using Microsoft.Reporting.WebForms;
using Microsoft.Office.Core;
using System.Text;
using System.Web.Services;
using System.Collections;
using System.IO;

namespace Catering.Catering_Page.richang
{
    public partial class SCP_Area : System.Web.UI.Page
    {
        public Dictionary<string, Catering_Model.TempGWJC> tempgwjclist = new Dictionary<string, Catering_Model.TempGWJC>();
        public List<string> onworklist = new List<string>();//总在岗位数
        public List<string> outworklist = new List<string>();//总歇岗位数
        public List<string> danweisumlist = new List<string>();//总单位数

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TextBox1.Attributes.Add("onfocus", "WdatePicker({dateFmt:'yyyy-MM-dd'})");//日期控件初始化
                loadPage();
                //if (!string.IsNullOrEmpty(Request.QueryString["PID"]))
                //{
                //    DataTable dt = dvfr.GetPersonModel(Request.QueryString["PID"]);
                //    GetReportDate(true);
                //    BingDivControls(dt);
                //    Request.QueryString["PID"].Remove(0);
                //}
                this.tongjibar.Visible = false;
            }
        }

        /// <summary>
        /// 作业区、班组初始化
        /// </summary>
        private void loadPage()
        {
            //frRules = new FromRecords();
            if (Session["User"] != null)
            {
                string userid = Session["PersonnelID"].ToString();
                int companyid = 0;
                int groupid = 0;
                int classid = 0;
                bool isAdmin = false;
                Bind_Data_to_Controls.GetCompanyByCurrentUser(userid, out companyid, out groupid, out classid, out isAdmin);
                Bind_Data_to_Controls.Bind_Area_ddl(ddl_Area, isAdmin, groupid, companyid);
                Bind_Data_to_Controls.Bind_Class_ddl(ddl_Area.SelectedItem, ddl_class, isAdmin, classid);
            }
        }


        /// <summary>
        /// 作业区人员动态统计
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string areaID = "";                   //作业区
            string classID = "";                  //班组
            DateTime searchDate = new DateTime(); //搜索日期

            areaID = this.ddl_Area.SelectedItem.Value;               //作业区
            classID = this.ddl_class.SelectedItem.Value;             //班组
            DateTime.TryParse(this.TextBox1.Text, out searchDate);
            if (searchDate.Year < 1949) return;

            #region 初始化

            this.div_workinglist.InnerHtml = "";
            this.PersonForMSN.InnerHtml = "<li>总人数：0人</li>";

            #endregion

            View_Monitor vm = new View_Monitor();



            #region 设置报表数据源

            string pid = "0";
            if (Session["UserID"] != null) pid = Session["UserID"].ToString();
            DataTable dt = vm.GetPeicanDongTaiByDate(pid, areaID, classID, searchDate, false);

            InitData(dt);

            #region 设置公司人数

            if (dt != null && dt.Rows.Count > 0)
            {
                StringBuilder renshutongji = new StringBuilder("");
                int countworking = dt.Select("State=1 ").Count(); //在岗人数
                int countall = dt.Rows.Count;   //公司总人数
                int countunworking = countall - countworking;//歇岗人数
                renshutongji.Append(string.Format("<li>总人数：{0}人(在岗{1}人，歇岗{2}人)</li>", countall, countworking, countunworking));
                //this.PersonForMSN.InnerHtml = string.Format("<li>总人数：{0}人，在岗{1}人，歇岗{2}人</li>", countall, countworking, countunworking);
                foreach (string tempName in danweisumlist)
                {
                    int temponcont = dt.Select("State=1 AND SName='" + tempName + "' ").Count();
                    int tempallcont = dt.Select("SName='" + tempName + "' ").Count();
                    renshutongji.Append(string.Format("<li>{3}：{0}人(在岗{1}人，歇岗{2}人)</li>", tempallcont, temponcont, (tempallcont - temponcont), tempName));
                }
                this.PersonForMSN.InnerHtml = renshutongji.ToString();
                this.tongjibar.Visible = true;
            }
            else
            {
                this.tongjibar.Visible = false;
            }


            #endregion

            #region 字符串提取

            if (tempgwjclist != null && tempgwjclist.Count > 0)
            {
                StringBuilder StrHtml = new StringBuilder("");

                #region 表头

                StrHtml.Append("<table border='0'  cellpadding='0' cellspacing='1' id='table_Person'>");
                StrHtml.Append("<thead>");
                StrHtml.Append("<tr>");
                StrHtml.Append("<th rowspan='2'>作业区</th>");
                StrHtml.Append("<th rowspan='2'>班组</th>");
                StrHtml.Append("<th rowspan='2'>服务单位</th>");
                StrHtml.Append("<th rowspan='2'>总人数</th>");
                //StrHtml.Append("<th>&nbsp;</th>");
                StrHtml.Append("<th colspan='" + (onworklist.Count + 1) + "'>在岗</th>");
                StrHtml.Append("<th colspan='" + (outworklist.Count + 1) + "'>歇岗</th>");
                StrHtml.Append("</tr>");
                StrHtml.Append("<tr>");
                StrHtml.Append("<th>人数</th>");
                for (int i = 0; i < onworklist.Count; i++)
                {
                    StrHtml.Append("<th>" + onworklist[i] + "</th>");
                }
                StrHtml.Append("<th>人数</th>");
                for (int i = 0; i < outworklist.Count; i++)
                {
                    StrHtml.Append("<th>" + outworklist[i] + "</th>");
                }
                StrHtml.Append("</tr>");
                StrHtml.Append("</thead>");
                StrHtml.Append("<tbody>");

                #endregion
                int bryhjFlag = 0;
                foreach (Catering_Model.TempGWJC modelgwjc in tempgwjclist.Values)//一个作业区
                {                    
                    //StrHtml.Append("<tr>");
                    if (modelgwjc != null && modelgwjc.GWModel != null && modelgwjc.GWModel.Count > 0)
                    {
                        int maxzyq = 0;//作业区最大行数=班组最大行数之和
                        StringBuilder tempbanzuStr = new StringBuilder("");
                        foreach (Catering_Model.TempGangWeiModel modelgangwei in modelgwjc.GWModel.Values)//一个班组
                        {                            
                            #region 一个班组

                            int maxbz = 1;//班组最大行数=单位的最大行数之和+1(人员合计行数+1)
                            int classperson = 0;
                            StringBuilder tempdanweiStr = new StringBuilder("");//单位行

                            //总人数◇
                            //在岗人数⊙
                            //歇岗人数○ 
                            int totle_personondw = 0;//单位在岗人数
                            int totle_personoutdw = 0;//单位歇岗人数
                            foreach (Catering_Model.TempFWDanWeiModel modeldanwei in modelgangwei.DanWeiList.Values)
                            {
                                #region 一个单位

                                int maxdw = 0;//单位最大行数
                                int personondw = 0;//单位在岗人数
                                int personoutdw = 0;//单位歇岗人数
                                if (modeldanwei != null && modeldanwei.PersonList != null && modeldanwei.PersonList.Count > 0)
                                {
                                    foreach (Catering_Model.TempPersonList modelperlist in modeldanwei.PersonList.Values)//一个岗位
                                    {
                                        //岗位的最大行数
                                        int tempmax = modelperlist.OnPersonNum > modelperlist.OutPersonNum ? modelperlist.OnPersonNum : modelperlist.OutPersonNum;
                                        maxdw = tempmax > maxdw ? tempmax : maxdw;
                                    }
                                }
                                maxbz = maxbz + maxdw;

                                StringBuilder workStr = new StringBuilder("");
                                
                                for (int j = 0; j < maxdw; j++)
                                {
                                    if (j > 0) workStr.Append("<tr>");
                                    if (j == 0)
                                    {
                                        workStr.Append("<th width='80' rowspan='" + maxdw + "'>" + modeldanwei.SName + "</th>");
                                        workStr.Append("<th width='50' rowspan='" + maxdw + "'>◇</th>");
                                        workStr.Append("<td rowspan='" + maxdw + "'>⊙</td>");
                                    }
                                    for (int i = 0; i < onworklist.Count; i++)
                                    {
                                        if (modeldanwei.PersonList.ContainsKey(onworklist[i]) && modeldanwei.PersonList[onworklist[i]].OnPersonNum > j)
                                        {
                                            workStr.Append("<td><a href=\"javascript:ShowUserInfo('" + modeldanwei.PersonList[onworklist[i]].OnPerson[j].PID + "')\">" + modeldanwei.PersonList[onworklist[i]].OnPerson[j].Name + "</a></td>");
                                            personondw++;
                                        }
                                        else
                                        {
                                            workStr.Append("<td>&nbsp;</td>");
                                        }
                                    }
                                    if (j == 0) workStr.Append("<td rowspan='" + maxdw + "'>○</td>");
                                    for (int i = 0; i < outworklist.Count; i++)
                                    {
                                        if (modeldanwei.PersonList.ContainsKey(outworklist[i]) && modeldanwei.PersonList[outworklist[i]].OutPersonNum > j)
                                        {
                                            workStr.Append("<td><a href=\"javascript:ShowUserInfo('" + modeldanwei.PersonList[outworklist[i]].OutPerson[j].PID + "')\">" + modeldanwei.PersonList[outworklist[i]].OutPerson[j].Name + "</a></td>");
                                            personoutdw++;
                                        }
                                        else
                                        {
                                            workStr.Append("<td>&nbsp;</td>");
                                        }
                                    }
                                    workStr.Append("</tr>");
                                }
                                totle_personondw += personondw;
                                totle_personoutdw += personoutdw;
                                #endregion

                                workStr.Replace("◇", (personondw + personoutdw).ToString());
                                workStr.Replace("⊙", personondw.ToString());
                                workStr.Replace("○", personoutdw.ToString());
                                tempdanweiStr.Append(workStr.ToString());
                                classperson += (personondw + personoutdw);
                            }
                            if (tempbanzuStr.Length == 0)
                            {
                                if( bryhjFlag == 0 )
                                {
                                    tempbanzuStr.Append("<th width='80' rowspan='" + maxbz + "'>" + modelgangwei.ClassName + "<br/>（" + classperson + "）</th><th width='80' rowspan='1'>人员合计</th>");
                                    tempbanzuStr.Append("<th width='50' rowspan='1'>" + dt.Rows.Count.ToString() + "</th>");
                                    tempbanzuStr.Append("<td rowspan='1'>" + dt.Select("State=1 ").Count().ToString() + "</td>");
                                    int onCount = 0;
                                    for (int i = 0; i < onworklist.Count; i++)
                                    {
                                        onCount = dt.Select("State=1 AND Name='" + onworklist[i] + "' ").Count();
                                        tempbanzuStr.Append("<td>" + onCount.ToString() + "</td>");
                                    }

                                    tempbanzuStr.Append("<td rowspan='1'>" + (dt.Rows.Count - dt.Select("State=1 ").Count()).ToString() + "</td>");
                                    int outCount = 0;
                                    for (int i = 0; i < outworklist.Count; i++)
                                    {
                                        onCount = dt.Select("State=1 AND Name='" + outworklist[i] + "' ").Count();
                                        outCount = dt.Select("Name='" + outworklist[i] + "' ").Count();
                                        tempbanzuStr.Append("<td>" + (outCount-onCount).ToString() + "</td>");
                                    } 
                                }
                                else
                                {
                                    tempbanzuStr.Append("<th width='80' rowspan='" + maxbz + "'>" + modelgangwei.ClassName + "<br/>（" + classperson + "）</th>");
                                }
                                
                                tempbanzuStr.Append("</tr><tr>");
                            }
                            else
                            {
                                if (bryhjFlag == 0)
                                {
                                    tempbanzuStr.Append("<tr><th width='80' rowspan='" + maxbz + "'>" + modelgangwei.ClassName + "<br/>（" + classperson + "）</th><th width='80' rowspan='1'>人员合计</th>");
                                    //</tr><tr>");
                                    tempbanzuStr.Append("<th width='50' rowspan='1'>" + dt.Rows.Count.ToString() + "</th>");
                                    tempbanzuStr.Append("<td rowspan='1'>" + dt.Select("State=1 ").Count().ToString() + "</td>");
                                    int onCount = 0;
                                    for (int i = 0; i < onworklist.Count; i++)
                                    {
                                        onCount = dt.Select("State=1 AND Name='" + onworklist[i] + "' ").Count();
                                        tempbanzuStr.Append("<td>" + onCount.ToString() + "</td>");
                                    }
                                    tempbanzuStr.Append("<td rowspan='1'>" + (dt.Rows.Count - dt.Select("State=1 ").Count()).ToString() + "</td>");
                                    int outCount = 0;
                                    for (int i = 0; i < outworklist.Count; i++)
                                    {
                                        onCount = dt.Select("State=1 AND Name='" + outworklist[i] + "' ").Count();
                                        outCount = dt.Select("Name='" + outworklist[i] + "' ").Count();
                                        tempbanzuStr.Append("<td>" + (outCount - onCount).ToString() + "</td>");
                                    }
                                }
                                else
                                {
                                    tempbanzuStr.Append("<tr><th width='80' rowspan='" + maxbz + "'>" + modelgangwei.ClassName + "<br/>（" + classperson + "）</th>");
                                }
                                tempbanzuStr.Append("</tr><tr>");
                            }
                            tempbanzuStr.Append(tempdanweiStr.ToString());
                            maxzyq = maxzyq + maxbz;
                            bryhjFlag++;
                            #endregion
                        }
                        StrHtml.Append("<tr><th width='70' rowspan='" + maxzyq + "'>" + modelgwjc.GroupName + "</th>" + tempbanzuStr.ToString());
                    }
                }
                StrHtml.Append("</tbody>");
                StrHtml.Append("</table>");
                this.div_workinglist.InnerHtml = StrHtml.ToString();
            }

            #endregion

            #endregion
        }

        private void InitData(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                #region 数据提取

                //Dictionary<string, Catering_Model.TempGWJC> tempgwjclist = new Dictionary<string, Catering_Model.TempGWJC>();
                //List<string> onworklist = new List<string>();//总在岗位数
                //List<string> outworklist = new List<string>();//总歇岗位数
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string tareaName = dt.Rows[i]["AreaName"].ToString();   //作业区
                    string tclassName = dt.Rows[i]["CName"].ToString();     //班组
                    string tpersonName = dt.Rows[i]["PName"].ToString();    //用户名
                    string tpName = dt.Rows[i]["Name"].ToString();          //岗位名称
                    string tsName = dt.Rows[i]["SName"].ToString();         //服务单位
                    int tpid = Convert.ToInt32(dt.Rows[i]["Pid"]);          //用户ID
                    int tstate = 0;
                    if (!(dt.Rows[i]["State"] is DBNull) && dt.Rows[i]["State"] != null) tstate = Convert.ToInt32(dt.Rows[i]["State"]);      //在岗状态

                    //更改岗位数
                    if (tstate == 1 && !onworklist.Contains(tpName)) onworklist.Add(tpName);
                    if (tstate != 1 && !outworklist.Contains(tpName)) outworklist.Add(tpName);
                    if ((!string.IsNullOrEmpty(tsName)) && (!danweisumlist.Contains(tsName))) danweisumlist.Add(tsName);

                    //人员信息
                    Catering_Model.TempPerson modelperson = new Catering_Model.TempPerson();
                    modelperson.Name = tpersonName;
                    modelperson.PID = tpid;

                    if (tempgwjclist.ContainsKey(tareaName))//作业区
                    {
                        if (tempgwjclist[tareaName].GWModel.ContainsKey(tclassName))//存在此班组
                        {
                            if (tempgwjclist[tareaName].GWModel[tclassName].DanWeiList.ContainsKey(tsName))//存在此单位
                            {
                                if (tempgwjclist[tareaName].GWModel[tclassName].DanWeiList[tsName].PersonList.ContainsKey(tpName))//存在此岗位
                                {
                                    if (tstate == 1)
                                    {
                                        tempgwjclist[tareaName].GWModel[tclassName].DanWeiList[tsName].PersonList[tpName].OnPerson.Add(tempgwjclist[tareaName].GWModel[tclassName].DanWeiList[tsName].PersonList[tpName].OnPersonNum, modelperson);
                                        tempgwjclist[tareaName].GWModel[tclassName].DanWeiList[tsName].PersonList[tpName].OnPersonNum++;
                                    }
                                    else
                                    {
                                        tempgwjclist[tareaName].GWModel[tclassName].DanWeiList[tsName].PersonList[tpName].OutPerson.Add(tempgwjclist[tareaName].GWModel[tclassName].DanWeiList[tsName].PersonList[tpName].OutPersonNum, modelperson);
                                        tempgwjclist[tareaName].GWModel[tclassName].DanWeiList[tsName].PersonList[tpName].OutPersonNum++;
                                    }
                                }
                                else//不存在此岗位
                                {
                                    Catering_Model.TempPersonList modelperlist = new Catering_Model.TempPersonList();
                                    modelperlist.GangWei = tpName;
                                    if (tstate == 1)
                                    {
                                        modelperlist.OnPerson.Add(modelperlist.OnPersonNum, modelperson);
                                        modelperlist.OnPersonNum++;
                                    }
                                    else
                                    {
                                        modelperlist.OutPerson.Add(modelperlist.OutPersonNum, modelperson);
                                        modelperlist.OutPersonNum++;
                                    }
                                    tempgwjclist[tareaName].GWModel[tclassName].DanWeiList[tsName].PersonList.Add(modelperlist.GangWei, modelperlist);
                                }
                            }
                            else//不存在此单位
                            {
                                Catering_Model.TempFWDanWeiModel modeldanwei = new Catering_Model.TempFWDanWeiModel();
                                Catering_Model.TempPersonList modelperlist = new Catering_Model.TempPersonList();
                                modelperlist.GangWei = tpName;
                                if (tstate == 1)
                                {
                                    modelperlist.OnPerson.Add(modelperlist.OnPersonNum, modelperson);
                                    modelperlist.OnPersonNum++;
                                }
                                else
                                {
                                    modelperlist.OutPerson.Add(modelperlist.OutPersonNum, modelperson);
                                    modelperlist.OutPersonNum++;
                                }
                                modeldanwei.SName = tsName;
                                modeldanwei.PersonList.Add(modelperlist.GangWei, modelperlist);
                                tempgwjclist[tareaName].GWModel[tclassName].DanWeiList.Add(modeldanwei.SName, modeldanwei);
                            }
                        }
                        else//不存在此班组
                        {
                            Catering_Model.TempGangWeiModel modelgangwei = new Catering_Model.TempGangWeiModel();
                            Catering_Model.TempFWDanWeiModel modeldanwei = new Catering_Model.TempFWDanWeiModel();
                            Catering_Model.TempPersonList modelperlist = new Catering_Model.TempPersonList();
                            modelperlist.GangWei = tpName;
                            if (tstate == 1)
                            {
                                modelperlist.OnPerson.Add(modelperlist.OnPersonNum, modelperson);
                                modelperlist.OnPersonNum++;
                            }
                            else
                            {
                                modelperlist.OutPerson.Add(modelperlist.OutPersonNum, modelperson);
                                modelperlist.OutPersonNum++;
                            }
                            modeldanwei.SName = tsName;
                            modeldanwei.PersonList.Add(modelperlist.GangWei, modelperlist);
                            modelgangwei.ClassName = tclassName;
                            modelgangwei.DanWeiList.Add(modeldanwei.SName, modeldanwei);
                            tempgwjclist[tareaName].GWModel.Add(modelgangwei.ClassName, modelgangwei);
                        }
                    }
                    else//不存在此作业区
                    {
                        Catering_Model.TempGWJC modelgwjc = new Catering_Model.TempGWJC();
                        Catering_Model.TempGangWeiModel modelgangwei = new Catering_Model.TempGangWeiModel();
                        Catering_Model.TempFWDanWeiModel modeldanwei = new Catering_Model.TempFWDanWeiModel();
                        Catering_Model.TempPersonList modelperlist = new Catering_Model.TempPersonList();
                        modelgwjc.GroupName = tareaName;
                        modelgangwei.ClassName = tclassName;
                        modeldanwei.SName = tsName;
                        modelperlist.GangWei = tpName;

                        if (tstate == 1)
                        {
                            modelperlist.OnPerson.Add(modelperlist.OnPersonNum, modelperson);
                            modelperlist.OnPersonNum++;
                        }
                        else
                        {
                            modelperlist.OutPerson.Add(modelperlist.OutPersonNum, modelperson);
                            modelperlist.OutPersonNum++;
                        }
                        modeldanwei.PersonList.Add(modelperlist.GangWei, modelperlist);
                        modelgangwei.DanWeiList.Add(modeldanwei.SName, modeldanwei);
                        modelgwjc.GWModel.Add(modelgangwei.ClassName, modelgangwei);
                        tempgwjclist.Add(modelgwjc.GroupName, modelgwjc);
                    }
                }
                dt.Dispose();

                #endregion
            }
        }

        protected void ddl_Area_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddl_Area.SelectedItem != null)
            {
                Bind_Data_to_Controls.Bind_Class_ddl(ddl_Area.SelectedItem, ddl_class);
            }
        }

        [WebMethod]
        public static ArrayList GetPersonnelInfo(string PID)
        {
            ArrayList list = new ArrayList();
            DataTable dt = new DataViewForReport().GetPersonModel(PID);
            if (dt != null && dt.Rows.Count > 0)
            {
                list.Add("OK");
                list.Add(dt.Rows[0]["Name"].ToString());
                list.Add(dt.Rows[0]["Sex"].ToString());
                DateTime old = DateTime.MinValue;
                DateTime.TryParse(dt.Rows[0]["Old"].ToString(), out old);
                if (old > DateTime.MinValue) list.Add(DateTime.Now.Year - old.Year); else list.Add(0);
                list.Add(dt.Rows[0]["No"].ToString());
                list.Add(dt.Rows[0]["Placeoforigin"].ToString());

                list.Add(dt.Rows[0]["Education"].ToString());
                list.Add(dt.Rows[0]["Professional"].ToString());
                list.Add(dt.Rows[0]["Title"].ToString());
                list.Add(dt.Rows[0]["Politicalaffiliation"].ToString());
                list.Add(dt.Rows[0]["QQ"].ToString());
                int SID = 0;
                string StrSID = dt.Rows[0]["ServiceUnitID"].ToString();
                Int32.TryParse(StrSID, out SID);
                if (SID > 0)
                {
                    Catering_Model.Service_Unit md = new Catering_Rules.Service_Unit().GetModel(SID);
                    if (md != null && !string.IsNullOrEmpty(md.SName)) list.Add(md.SName); else list.Add("");
                }
                else
                {
                    list.Add("");
                }
                list.Add(dt.Rows[0]["Tel"].ToString());
                list.Add(dt.Rows[0]["Email"].ToString());
                list.Add(dt.Rows[0]["ADD"].ToString());
                list.Add(dt.Rows[0]["back"].ToString());
            }
            return list;
        }

        protected void Btn_Excel_Click(object sender, EventArgs e)
        {
            string areaID = "";                   //作业区
            string classID = "";                  //班组
            DateTime searchDate = new DateTime(); //搜索日期

            areaID = this.ddl_Area.SelectedItem.Value;               //作业区
            classID = this.ddl_class.SelectedItem.Value;             //班组
            DateTime.TryParse(this.TextBox1.Text, out searchDate);

            View_Monitor vm = new View_Monitor();
            string pid = "0";
            if (Session["UserID"] != null) pid = Session["UserID"].ToString();
            DataTable dt = vm.GetPeicanDongTaiByDate(pid, areaID, classID, searchDate, false);

            InitData(dt);

            if (onworklist.Count <= 0 && outworklist.Count <= 0) return;
            Microsoft.Office.Interop.Excel.Application excel1 = null;
            Microsoft.Office.Interop.Excel._Worksheet ws = null;
            Microsoft.Office.Interop.Excel.Range canbie = null;
            string stick = DateTime.Now.Ticks.ToString();

            try
            {
                excel1 = new Microsoft.Office.Interop.Excel.Application();
                excel1.Application.Workbooks.Add(true);//引用Excel工作簿
                //Microsoft.Office.Interop.Excel._Worksheet ws = new Microsoft.Office.Interop.Excel.WorksheetClass();
                ws = new Microsoft.Office.Interop.Excel.WorksheetClass();
                ws = (Microsoft.Office.Interop.Excel._Worksheet)excel1.ActiveSheet;//获取当前活动工作薄
                //Microsoft.Office.Interop.Excel.Range canbie;//创建Range操作一行作为标题
                excel1.Visible = false;

                #region  表头

                canbie = ws.get_Range(ws.Cells[1, 1], ws.Cells[1, onworklist.Count + outworklist.Count + 6]);
                canbie.MergeCells = true;//合并单元格
                canbie.RowHeight = 30;
                canbie.Font.Bold = 2;
                ws.Cells[1, 1] = DateTime.Now.ToLongDateString() + "班组人员动态";

                #endregion

                #region 第一行标题

                canbie = ws.get_Range(ws.Cells[2, 1], ws.Cells[3, 1]);
                canbie.MergeCells = true;//合并单元格
                //canbie.HorizontalAlignment = XlVAlign.xlVAlignCenter;//居中
                canbie.Font.Bold = 2;
                ws.Cells[2, 1] = "作业区";

                canbie = ws.get_Range(ws.Cells[2, 2], ws.Cells[3, 2]);
                canbie.MergeCells = true;//合并单元格
                //canbie.HorizontalAlignment = XlVAlign.xlVAlignCenter;//居中
                canbie.Font.Bold = 2;
                ws.Cells[2, 2] = "班组";

                canbie = ws.get_Range(ws.Cells[2, 3], ws.Cells[3, 3]);
                canbie.MergeCells = true;//合并单元格
                //canbie.HorizontalAlignment = XlVAlign.xlVAlignCenter;//居中
                canbie.Font.Bold = 2;
                ws.Cells[2, 3] = "服务单位";

                canbie = ws.get_Range(ws.Cells[2, 4], ws.Cells[3, 4]);
                canbie.MergeCells = true;//合并单元格
                //canbie.HorizontalAlignment = XlVAlign.xlVAlignCenter;//居中
                canbie.Font.Bold = 2;
                ws.Cells[2, 4] = "总人数";

                canbie = ws.get_Range(ws.Cells[2, 5], ws.Cells[2, onworklist.Count + 5]);
                canbie.MergeCells = true;//合并单元格
                //canbie.HorizontalAlignment = XlVAlign.xlVAlignCenter;//居中
                canbie.Font.Bold = 2;
                ws.Cells[2, 5] = "在岗";

                canbie = ws.get_Range(ws.Cells[2, onworklist.Count + 6], ws.Cells[2, onworklist.Count + outworklist.Count + 6]);
                canbie.MergeCells = true;//合并单元格
                //canbie.HorizontalAlignment = XlVAlign.xlVAlignCenter;//居中
                canbie.Font.Bold = 2;
                ws.Cells[2, onworklist.Count + 6] = "歇岗";

                ws.Cells[3, 5] = "人数";

                for (int i = 0; i < onworklist.Count; i++)
                {
                    ws.Cells[3, i + 6] = onworklist[i];
                }

                ws.Cells[3, onworklist.Count + 6] = "人数";

                for (int i = 0; i < outworklist.Count; i++)
                {
                    ws.Cells[3, i + onworklist.Count + 7] = outworklist[i];
                }

                canbie = ws.get_Range(ws.Cells[3, 5], ws.Cells[3, onworklist.Count + outworklist.Count + 6]);
                canbie.Font.Bold = 2;

                #endregion

                #region 主要内容

                int zyqhangshu = 4;//作业区行数
                int bzhangshu = zyqhangshu;//班组行数
                int canbiehangshu = bzhangshu;//服务单位行数

                if (tempgwjclist != null && tempgwjclist.Count > 0)
                {
                    foreach (Catering_Model.TempGWJC modelgwjc in tempgwjclist.Values)//一个作业区
                    {
                        //StrHtml.Append("<tr>");
                        if (modelgwjc != null && modelgwjc.GWModel != null && modelgwjc.GWModel.Count > 0)
                        {
                            int maxzyq = 0;//作业区最大行数=班组最大行数之和

                            foreach (Catering_Model.TempGangWeiModel modelgangwei in modelgwjc.GWModel.Values)//一个班组
                            {
                                #region 一个班组

                                int maxbz = 0;//班组最大行数=单位的最大行数之和

                                //总人数◇
                                //在岗人数⊙
                                //歇岗人数○
                                foreach (Catering_Model.TempFWDanWeiModel modeldanwei in modelgangwei.DanWeiList.Values)
                                {
                                    #region 一个单位

                                    int maxdw = 0;//单位最大行数
                                    int personondw = 0;//单位在岗人数
                                    int personoutdw = 0;//单位歇岗人数
                                    if (modeldanwei != null && modeldanwei.PersonList != null && modeldanwei.PersonList.Count > 0)
                                    {
                                        foreach (Catering_Model.TempPersonList modelperlist in modeldanwei.PersonList.Values)//一个岗位
                                        {
                                            //岗位的最大行数
                                            int tempmax = modelperlist.OnPersonNum > modelperlist.OutPersonNum ? modelperlist.OnPersonNum : modelperlist.OutPersonNum;
                                            maxdw = tempmax > maxdw ? tempmax : maxdw;
                                        }
                                    }
                                    maxbz = maxbz + maxdw;

                                    //服务单位
                                    canbie = ws.get_Range(ws.Cells[canbiehangshu, 3], ws.Cells[canbiehangshu + maxdw - 1, 3]);
                                    canbie.MergeCells = true;//合并单元格
                                    ws.Cells[canbiehangshu, 3] = modeldanwei.SName;

                                    for (int j = 0; j < maxdw; j++)
                                    {
                                        for (int i = 0; i < onworklist.Count; i++)
                                        {
                                            if (modeldanwei.PersonList.ContainsKey(onworklist[i]) && modeldanwei.PersonList[onworklist[i]].OnPersonNum > j)
                                            {
                                                ws.Cells[canbiehangshu + j, i + 6] = modeldanwei.PersonList[onworklist[i]].OnPerson[j].Name;
                                                personondw++;
                                            }
                                        }
                                        for (int i = 0; i < outworklist.Count; i++)
                                        {
                                            if (modeldanwei.PersonList.ContainsKey(outworklist[i]) && modeldanwei.PersonList[outworklist[i]].OutPersonNum > j)
                                            {
                                                ws.Cells[canbiehangshu + j, i + 7 + onworklist.Count] = modeldanwei.PersonList[outworklist[i]].OutPerson[j].Name;
                                                personoutdw++;
                                            }
                                        }
                                    }

                                    //总人数
                                    canbie = ws.get_Range(ws.Cells[canbiehangshu, 4], ws.Cells[canbiehangshu + maxdw - 1, 4]);
                                    canbie.MergeCells = true;//合并单元格
                                    ws.Cells[canbiehangshu, 4] = personondw + personoutdw;

                                    //在岗人数
                                    canbie = ws.get_Range(ws.Cells[canbiehangshu, 5], ws.Cells[canbiehangshu + maxdw - 1, 5]);
                                    canbie.MergeCells = true;//合并单元格
                                    ws.Cells[canbiehangshu, 5] = personondw;

                                    //歇岗人数
                                    canbie = ws.get_Range(ws.Cells[canbiehangshu, 6 + onworklist.Count], ws.Cells[canbiehangshu + maxdw - 1, 6 + onworklist.Count]);
                                    canbie.MergeCells = true;//合并单元格
                                    ws.Cells[canbiehangshu, 6 + onworklist.Count] = personoutdw;

                                    canbiehangshu = canbiehangshu + maxdw;
                                    #endregion
                                }

                                //班组
                                canbie = ws.get_Range(ws.Cells[bzhangshu, 2], ws.Cells[bzhangshu + maxbz - 1, 2]);
                                canbie.MergeCells = true;//合并单元格
                                ws.Cells[bzhangshu, 2] = modelgangwei.ClassName;
                                bzhangshu = bzhangshu + maxbz;
                                maxzyq = maxzyq + maxbz;
                                #endregion
                            }
                            //作业区
                            canbie = ws.get_Range(ws.Cells[zyqhangshu, 1], ws.Cells[zyqhangshu + maxzyq - 1, 1]);
                            canbie.MergeCells = true;//合并单元格
                            ws.Cells[zyqhangshu, 1] = modelgwjc.GroupName;
                            zyqhangshu = zyqhangshu + maxzyq;
                        }
                    }
                    //自适应、边框、居中
                    ws.Cells.EntireColumn.AutoFit();
                    canbie = ws.get_Range(ws.Cells[1, 1], ws.Cells[zyqhangshu - 1, onworklist.Count + outworklist.Count + 6]);
                    canbie.HorizontalAlignment = XlVAlign.xlVAlignCenter;
                    canbie.Borders.LineStyle = 1;
                }

                #endregion

                if (!Directory.Exists(Server.MapPath("../../xls_template"))) Directory.CreateDirectory(Server.MapPath("../../xls_template"));
                ws.SaveAs(Server.MapPath("../../xls_template") + "\\" + stick + ".xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlAddIn8);
            }
            finally
            {
                if (excel1 != null)
                {
                    if (ws != null)
                    {
                        if (canbie != null)
                        {
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(canbie);
                            canbie = null;

                        }
                        System.Runtime.InteropServices.Marshal.ReleaseComObject(ws);
                        ws = null;
                    }
                    excel1.Application.Workbooks.Close();
                    excel1.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(excel1);
                    Toolss.KillExcel(excel1);
                    excel1 = null;
                    GC.Collect();
                }
            }

            #region 发送

            try
            {
                string path = Server.MapPath("../../xls_template") + "\\" + stick + ".xls";
                System.IO.FileInfo file = new System.IO.FileInfo(path);
                Response.Clear();
                Response.Charset = "GB2312";
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                // 添加头信息，,弹出另存为窗口  
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
                //让浏览器头显示下载信息  
                Response.AddHeader("Content-Length", file.Length.ToString());
                // 指定返回一个不能被客户端读取的流，下载  
                Response.ContentType = "application/ms-excel";
                //把文件流下载到客户端  
                Response.WriteFile(file.FullName);
                Toolss.DeleteFile(Server.MapPath("../../xls_template") + "\\" + stick + ".xls");
                // 停止页面的执行
                Response.End();
            }
            catch
            {
            }

            #endregion
        }
    }
}